import pyecharts.options as opts
from pyecharts.charts import Line
# from pyecharts.globals import ThemeType
import pandas as pd
import numpy as np

class LineChart_temp_1year():
    
    def __init__(self, dataset, place=0, year='2015'):
        self.paint_data=dataset
        self.place=place #九寨沟——0；四姑娘山——1
        self.year=year

        #主程序入口
        self.main()
    
    
    #主程序入口函数实现
    def main(self):
        
        # 调用获取数据函数
        self.getData()
        # 调用绘图函数
        self.paint()
        
        
    def getData(self):
            
        df=self.paint_data.loc[:,['date','最高温度','最低温度']] #提取日期和气温数据
        # df["最高温度"] = df["最高温度"].str.replace("℃", "")
        # df["最低温度"] = df["最低温度"].str.replace("℃", "")
        self.paint_data=df.loc[df['date'].str.contains(self.year)] #筛选指定年份的数据 
        # print(self.paint_data)


    def paint(self):
        day_name_list = np.array(self.paint_data.loc[:,'date']).tolist()
        high_temperature = np.array(self.paint_data.loc[:,'最高温度']).tolist()
        low_temperature = np.array(self.paint_data.loc[:,'最低温度']).tolist()

        if self.place==0:
            title='九寨沟'+self.year+'年气温变化'
        else:
            title='小金'+self.year+'年气温变化'        
        
        fileName='4.2 (L1400)'+title+"折线图.html"
        
        (
            # Line(init_opts=opts.InitOpts(theme=ThemeType.DARK, width="1400px", height="600px"))
            Line(init_opts=opts.InitOpts(width="1400px", height="600px"))
            .add_xaxis(xaxis_data=day_name_list)
            .add_yaxis(
                series_name="最高气温",
                y_axis=high_temperature,
                markpoint_opts=opts.MarkPointOpts(
                    data=[
                        opts.MarkPointItem(type_="max", name="最大值"),
                        opts.MarkPointItem(type_="min", name="最小值"),
                    ]
                ),
                markline_opts=opts.MarkLineOpts(
                    data=[opts.MarkLineItem(type_="average", name="平均值")]
                ),
            )
            .add_yaxis(
                series_name="最低气温",
                y_axis=low_temperature,
                markpoint_opts=opts.MarkPointOpts(
                    data=[opts.MarkPointItem(value=-2, name="年最低", x=1, y=-1.5)]
                ),
                markline_opts=opts.MarkLineOpts(
                    data=[
                        opts.MarkLineItem(type_="average", name="平均值"),
                        opts.MarkLineItem(symbol="none", x="90%", y="max"),
                        opts.MarkLineItem(symbol="circle", type_="max", name="最高点"),
                    ]
                ),
            )
            .set_global_opts(
                title_opts=opts.TitleOpts(title=title),
                tooltip_opts=opts.TooltipOpts(trigger="axis"),
                toolbox_opts=opts.ToolboxOpts(is_show=True),
                xaxis_opts=opts.AxisOpts(type_="category", boundary_gap=False),
            )
            .render('resultFile/'+fileName)
        )
        
        print(fileName+"已生成！")
